//
//  SHCollectionViewController.m
//  UI_CarHome
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHCollectionViewController.h"
#import "SHHandle.h"
#import "SHModel.h"
#import "SHDetailViewController.h"
#import "SHCollectionTableCell.h"
#pragma mark 4.签订协议
@interface SHCollectionViewController ()<UITableViewDataSource, UITableViewDelegate, SHDetailViewControllerDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *collectionArr;
@property(nonatomic, retain)UIImageView *imageView;
@end

@implementation SHCollectionViewController

- (void)dealloc
{
    [_imageView release];
    [_collectionArr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.title = @"我的收藏";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 100 * HEI;
    self.tableView.separatorStyle = NO;
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isNight:) name:@"isNight" object:nil];
    
    [self createData];
    [self createHeaderAndFooter];
    
}
- (void)isNight:(NSNotification *)notification {
    [self.tableView reloadData];
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.tableView.separatorStyle = SEPARATORSTYLE;
}
- (void)createHeaderAndFooter {

}

- (void)createData {
    if (self.imageView) {
        [self.imageView removeFromSuperview];
    }
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CarHome/carHome.plist"];
    self.collectionArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (self.collectionArr.count == 0) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [self.view addSubview:self.imageView];
        [_imageView release];
        self.imageView.image = [UIImage imageNamed:@"lu"];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    SHCollectionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[SHCollectionTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    
    SHModel *model = self.collectionArr[indexPath.row];
    cell.tittleLabel.text = model.title;
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.smallpic] placeholderImage:PLACEPICTURE];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.collectionArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SHDetailViewController *detail = [[SHDetailViewController alloc] init];
    detail.model = self.collectionArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
#pragma 5.设置代理人
    detail.delegate = self;
}
#pragma 6.实现协议方法
- (void)takeValue:(SHModel *)strValue {
    // 把传过来的model从数组中去除
    [self.collectionArr removeObject:strValue];
    // 刷新tableview
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
